package com.frxuexi.demo.diff


import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.DiffUtil.DiffResult
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.frxuexi.demo.R


class DiffActivity : AppCompatActivity() {
    private lateinit var mDatas: ArrayList<TestBean>
    private var mRv: RecyclerView? = null
    private var mAdapter: DiffAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_diff)
        initData()
        mRv = findViewById<View>(R.id.rv) as RecyclerView
        mRv!!.layoutManager = LinearLayoutManager(this)
        mAdapter = DiffAdapter(this, mDatas)
        mRv!!.adapter = mAdapter
    }

    private fun initData() {
        mDatas = ArrayList()
        mDatas.add(TestBean("吴大哥", "我的内容是一样的。。。",
            R.drawable.ic_launcher_background, 34456, 865))
        mDatas.add(TestBean("吴大哥", "我的内容是一样的。。。",
            R.drawable.ic_launcher_background, 2345, 98765))
        mDatas.add(TestBean("吴大哥", "我的内容是一样的。。。",
            R.drawable.ic_launcher_background, 87, 45))
        mDatas.add(TestBean("吴大哥", "我的内容是一样的。。。",
            R.drawable.ic_launcher_background, 2368435, 7587))
    }

    /**
     * 模拟刷新操作
     *
     * @param view
     */
    fun onRefresh(view: View?) {
        try {
            mNewDatas = ArrayList()
            for (bean in mDatas!!) {
                mNewDatas.add(bean.clone()) //clone一遍旧数据 ，模拟刷新操作
            }
            mNewDatas.get(0).lookNumber = (Math.random() * 200).toInt()
//            mNewDatas.get(0).commentNumber = (Math.random() * 200).toInt()
//            mNewDatas.get(1).lookNumber = (Math.random() * 200).toInt()
//            mNewDatas.get(1).commentNumber = (Math.random() * 200).toInt()
//            mNewDatas.get(2).lookNumber = (Math.random() * 200).toInt()
//            mNewDatas.get(2).commentNumber = (Math.random() * 200).toInt()
//            mNewDatas.get(3).lookNumber = (Math.random() * 200).toInt()
//            mNewDatas.get(3).commentNumber = (Math.random() * 200).toInt()

            //新宠
            //利用DiffUtil.calculateDiff()方法，传入一个规则DiffUtil.Callback对象，和是否检测移动item的 boolean变量，得到DiffUtil.DiffResult 的对象
            Thread { //放在子线程中计算DiffResult
                val diffResult = DiffUtil.calculateDiff(DiffCallBack(mDatas, mNewDatas), true)
                val message = mHandler.obtainMessage(1)
                message.obj = diffResult //obj存放DiffResult
                message.sendToTarget()
            }.start()
            //mAdapter.notifyDataSetChanged();//以前普通青年的我们只能这样，现在我们是文艺青年了，有新宠了
        } catch (e: CloneNotSupportedException) {
            e.printStackTrace()
        }
    }

    private lateinit var mNewDatas: MutableList<TestBean> //增加一个变量暂存newList

    @SuppressLint("HandlerLeak")
    private val mHandler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                1 -> {
                    //取出Result
                    val diffResult = msg.obj as DiffResult
                    //利用DiffUtil.DiffResult对象的dispatchUpdatesTo（）方法，传入RecyclerView的Adapter，轻松成为文艺青年
                    diffResult.dispatchUpdatesTo(mAdapter!!)
                    //别忘了将新数据给Adapter
                    mDatas = mNewDatas as ArrayList<TestBean>
                    mAdapter!!.setDatas(mDatas)
                }
            }
        }
    }
}

